﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EngineLayer.Services
{ 
    public class Products : DBEntities, IProducts, ICategories
    {
        public string description { get; set; }
        public bool canDiscount { get; set; }
        public double price { get; set; }

        public IEnumerable<object> GetAllProducts()
        {
            return db.Products;
        }

        public IEnumerable<object> GetAllCategories()
        {
            return db.ProductCategorys.OrderBy(c => c.Name);
        } 
    
        public IEnumerable<object> FilterByProductNameOrID(string filter)
        {
            int NumericFilter;
            bool IsNumeric = int.TryParse(filter, out NumericFilter);

            if (IsNumeric)
                return db.Products.Where(h => h.Id == NumericFilter);
            else
                return db.Products.Where(h => h.Name.StartsWith(filter));
        
        }

        public IEnumerable<KeyValuePair<int,Products>> GetAllProductsShort()
        {
            return db.Products.OrderBy(s => s.Id)
                .AsEnumerable()
                .Select(t => new KeyValuePair<int, Products>(t.Id, new Products { description = t.Name, canDiscount = true, price = t.Price }))
                .ToList();
        }
   
        public bool Exist(int Id)
        {
            return db.Products.Any(n => n.Id == Id);
        }
    }
}

